import os
import random

if __name__ == '__main__':

    # TODO 6 输出aaaff
    t6 = (('a', 'b', 'c'), ('aa', 'bb', 'cc'), ('dd', 'ee', 'ff'))
    print(t6[0][0] + t6[1][0] + t6[2][2])

    # TODO 7 取值：3.14, aa, (113, 114)
    t7 = (11, 3.14, 'heaao', (110, 'abc', (112, 113, 114)))
    print(t7[1], t7[2][2:4], t7[3][2][1:3])

    # TODO 8 输出为'中台多濛深'
    str_test = '情深深雨濛濛，多少楼台烟雨中'
    str_out = str_test[-1] + str_test[-4] + str_test[-7] + str_test[4] + str_test[1]
    print(str_out)

    # TODO 9 输出为"D:\1学习资料\小泽.avi"
    file_path = "D:\\1学习资料"  # 需要在电脑对应的路径下建立好测试数据
    try:
        file_list = os.listdir(file_path)  # 获取该路径下的文件列表，需要导入os库
        file_name = '小泽.avi'
        file_name_out = "文件未找到"
        if file_list:  # 如果该路径下有文件的话，就进行寻找
            for file_name_temp in file_list:
                if file_name_temp == file_name:  # 如果与目标文件匹配上就拼接路径
                    file_name_out = file_path + "\\" + file_name_temp
                    break
        print(file_name_out)
    except FileNotFoundError as e:
        print("路径不存在")

    # TODO 10 要求输出为[1,2,3,4,5,6,7]
    t10 = [1, 1, 2, 2, 3, 3, 4, 4]
    t10.clear()
    for i in range(1, 8):
        t10.append(i)
    print(t10)

    # TODO 11 列表追加1个值，追加10个值
    t11 = []
    t11_temp = []
    t11.append(0)  # 追加1个值
    for i in range(1, 11):  # 用for循环创建一个1-10的列表
        t11_temp.append(i)
    t11.extend(t11_temp)  # 追加多个值（追加列表）
    print(t11)

    # TODO 12 列表是[1,2,3,4],输出为['a','b',3,4]
    t12 = [1, 2, 3, 4]
    t12[0] = 'a'
    t12[1] = 'b'
    print(t12)

    # TODO 13 列表[11,10,3,8,5,12,14],要求输出为[14, 12, 11, 10, 8, 5, 3]
    t13 = [11, 10, 3, 8, 5, 12, 14]
    t13.sort()  # 升序排序   sorted()不改变原始结构
    t13.reverse()  # 倒序
    # print(t13[::-1])  # 倒序打印
    print(t13)

    # TODO 14 列表[1,2,1,3,4]要求去除1，输出[1,2,3,4]
    t14 = [1, 2, 1, 3, 4]
    t14.remove(1)  # 移除一个 1
    # t14.pop(0)  # t14.pop(index) 移除指定位置元素
    t14.sort()  # 排序
    print(t14)

    # TODO 15 空字典 添加price的价格有110,112,143,534,533
    t15 = {}
    t15['price'] = [110, 112, 143, 534, 533]
    print(t15)

    # TODO 16 输出 "山哥3,48,girl"
    data = {
        "code": 0,
        "data": [
            {"name": "山哥1", "age": 18, "username": "qwen1", 'loveStory': [{"2012": "girl", "2014": "boy"}],
             "weixin": "qwentest123", "register_time": "2022-04-07"},
            {"name": "山哥2", "age": 28, "username": "qwen2", "weixin": "qwentest123", "register_time": "2022-04-07"},
            {"name": "山哥3", "age": 38, "username": "qwen3", "weixin": "qwentest123", "register_time": "2022-04-07"},
            {"name": "山哥4", "age": 48, "username": "qwen4", "weixin": "qwentest123", "register_time": "2022-04-07"},
            {"name": "山哥5", "age": 58, "username": "qwen5", "weixin": "qwentest123", "register_time": "2022-04-07"}
        ],
        'count': 5
    }
    data16_1 = data.get("data")[2].get("name")
    data16_2 = data.get("data")[3].get("age")
    data16_3 = data.get("data")[0].get("loveStory")[0].get("2012")
    print(data16_1, data16_2, data16_3)

    # TODO 17 输出["2012","2014"],['girl','boy'],[("2012","girl"),("2014","boy")]
    data17_key = data.get("data")[0].get("loveStory")[0].keys()
    data17_key_list = []
    for i in data17_key:
        data17_key_list.append(i)
    data17_value = data.get("data")[0].get("loveStory")[0].values()
    data17_value_list = []
    for i in data17_value:
        data17_value_list.append(i)
    # data17_3 = data.get("data")[0].get("loveStory")[0]
    data17_3 = []
    for i in range(len(data17_key_list)):
        item = (data17_key_list[i], data17_value_list[i])
        data17_3.append(item)
    print(data17_key_list, data17_value_list, data17_3)

    # TODO 18 向data这个字典中增加一个字段msg，且内容为空
    data["msg"] = ""
    print("msg = " + data.get("msg"))

    # TODO 19 有内容为"13632111456,男,18,2014"，请转换成列表
    t19 = "13632111456,男,18,2014"
    list_19 = t19.split(',')  # 以“,”分割为列表
    # list_19_2 = list(t19)  # 以每一个字符转换为列表
    # tuple(t19)  转换为元组
    # set(list_19)  转换为集合，会自动去重
    print(list_19)

    # TODO 20 有内容为['a','b','c','d'],请输出为"abcd"
    t20 = ['a', 'b', 'c', 'd']
    t20_list = "".join(t20)  # 列表转字符串
    t20_list2 = ""
    for i in t20:  # 或者一个个字符拼接
        t20_list2 += i
    print(t20_list)
    print(t20_list2)

    # TODO 21 有内容为"3.1454"，请输出为int型的3
    t21 = "3.1454"
    t21_float = float(t21)  # 先转为浮点型
    t21_int = int(t21_float)  # 再转为整数型
    print(t21_int)

    # TODO 22 字典a = {1:2,3:4},字典b为{0.5:5}，请合并两个字典
    a = {1: 2, 3: 4}
    b = {0.5: 5}
    # a.update(b)
    # print(a)
    # 如果不破坏原有的字典，应拷贝字典进行合并
    c = a.copy()
    c.update(b)
    print(c)

    # TODO 23 集合[110,112,113] 输出字符串类型的110112
    t23_set = {110, 112, 113}
    # print(type(t23_set))
    t23_set.remove(113)
    t23_list = list(t23_set)
    t23_list.sort()
    t23_set_str = ""
    for i in t23_list:
        t23_set_str += str(i)
    print(t23_set_str)

    # TODO 24 元组、字符串、列表、字典有什么区别？
    """
        数据类型比较	 字符串  列表  元组  字典  集合
        是否有序	     是	    是	  是	否	 否
        是否可修改	 否	    是	  不	是	 是
    """

    # TODO 25 列表中有6个数，取出里面最大的三个数
    t25 = [24, 56, 74, 12, 65, 8]
    t25.sort()
    t25.reverse()
    print("排序后数据列表：", t25)
    t25_out = []
    count = 0  # 用来计数的字段
    for i in t25:
        if count >= 3:  # 只添加前面三个数
            break
        t25_out.append(i)
        count = count + 1
    print(t25_out)

    # TODO 26 python有哪些数据类型，其中那几个数据类型做测试时经常遇到，这些数据类型的处理常见的方法有哪些啊？
    """
        数字类型，字符串，列表，元组，字典，集合
        ...
    """

    # TODO 28 输入三个整数x,y,z，请把这三个数由小到大输出
    t28 = input("请输入三个数字（使用空格隔开）:").split(' ')
    t28_int_list = []
    try:
        for i in t28:
            t28_int_list.append(int(i))  # 把输入的列表转成int列表，不然可能排序会出问题
        t28_int_list.sort()
        print(t28_int_list)
    except Exception as e:
        print("输入格式错误")

    # TODO 29 用户输入年份year, 判断是否为闰年?
    t29 = input("请输入年份（2022）:")
    t29_int = int(t29)  # 把输入转成 int  不能四则运算报错
    if (t29_int % 4 == 0 and t29_int % 100 != 0) or t29_int % 400 == 0:
        print(t29_int, "是闰年")
    else:
        print(t29_int, "不是闰年")

    # TODO 28 输入年、月，输出本月有多少天。合理选择分支语句完成设
    t28_input = input("请输入年份与月份（2022 7,空格隔开,回车结束）:").split(' ')
    is_run_year = False
    year = int(t28_input[0])
    month = t28_input[1]
    # is_run_year = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
        is_run_year = True
    else:
        is_run_year = False
    month_days = {
        "run":
            {"1": "31", "2": "29", "3": "31", "4": "30", "5": "31", "6": "30",
             "7": "31", "8": "31", "9": "30", "10": "31", "11": "30", "12": "31"},
        "ping":
            {"1": "31", "2": "28", "3": "31", "4": "30", "5": "31", "6": "30",
             "7": "31", "8": "31", "9": "30", "10": "31", "11": "30", "12": "31"}
    }
    if is_run_year:
        print("本月：", month_days.get("run").get(month), "天")
    else:
        print("本月：", month_days.get("ping").get(month), "天")

    """
    29.
    1.从控制台输入要出的拳 --石头1 剪刀2 布3
    2.电脑随机出拳
    3.比较胜负
    """
    try:
        t29_input = input("请输入(1-3):")
        t29_input_int = int(t29_input)
        data = {"1": "石头", "2": "剪刀", "3": "布"}
        if t29_input_int > 3 or t29_input_int < 1:
            print("输入有误，请输入1-3之间的数字")
        else:
            computer = random.randint(1, 3)
            if t29_input_int == 1:
                if computer == 1:
                    print("You: ", data.get(t29_input), ", PC：", data.get(str(computer)), " 平局")
                elif computer == 2:
                    print("You: ", data.get(t29_input), ", PC：", data.get(str(computer)), " 你赢了")
                else:
                    print("You: ", data.get(t29_input), ", PC：", data.get(str(computer)), " 你输了")
            elif t29_input_int == 2:
                if computer == 1:
                    print("You: ", data.get(t29_input), ", PC：", data.get(str(computer)), " 你输了")
                elif computer == 2:
                    print("You: ", data.get(t29_input), ", PC：", data.get(str(computer)), " 平局")
                else:
                    print("You: ", data.get(t29_input), ", PC：", data.get(str(computer)), " 你赢了")
            else:
                if computer == 1:
                    print("You: ", data.get(t29_input), ", PC：", data.get(str(computer)), " 你赢了")
                elif computer == 2:
                    print("You: ", data.get(t29_input), ", PC：", data.get(str(computer)), " 你输了")
                else:
                    print("You: ", data.get(t29_input), ", PC：", data.get(str(computer)), " 平局")
    except Exception as e:
        print("输入有误，请输入1-3之间的数字")

    """
    30.输入1个成绩，判断学生成绩的等级
    85以上 优秀
    70～85 良好
    60～70 及格
    60以下 不及格
    """
    t30_input = input("请输入成绩:")
    t30_input_int = int(t30_input)
    if t30_input_int >= 85:
        print("优秀")
    elif t30_input_int >= 70:
        print("良好")
    elif t30_input_int >= 60:
        print("及格")
    else:
        print("不及格")

    # TODO 31.输入一个数，判断它是否能被3或5或7整除
    t31_input = input("请输入一个整数:")
    t31_input_int = int(t31_input)
    is_357 = False
    if t31_input_int % 3 == 0:
        print(t31_input_int, "可以被3整除")
        is_357 = True
    if t31_input_int % 5 == 0:
        print(t31_input_int, "可以被5整除")
        is_357 = True
    if t31_input_int % 7 == 0:
        print(t31_input_int, "可以被7整除")
        is_357 = True
    if not is_357:
        print(t31_input_int, "不可以被3或5或7整除")

    # TODO 32 规定：购物不足50元的按原价付款，超过50不足100的按九折付款。超过100元的，超过部分按八折付款。编一程序完成超市的自动计费的工作。
    t32_input = input("请输入一个付款金额:")
    t32_input_int = int(t32_input)
    money = 0
    if t32_input_int > 100:
        money = 50 + 50 * 0.9 + (t32_input_int - 100) * 0.8
    elif t32_input_int > 50:
        money = 50 + (t32_input_int - 50) * 0.9
    else:
        money = t32_input_int
    print("优惠后支付金额为：", money)

    """
    33.对年龄段进行判断(只能使用If 和else实现）
    儿童 1-7
    少年 8- 17
    青年 18-30
    中年 31-59
    老年 60-100
    寿星 100-500
    其它 年龄异常
    """
    t33_input = input("请输入年龄:")
    try:
        t33_input_int = int(t33_input)
        if t33_input_int > 500:
            print("年龄异常")
        elif t33_input_int >= 100:
            print("寿星")
        elif t33_input_int >= 60:
            print("老年")
        elif t33_input_int >= 31:
            print("中年")
        elif t33_input_int >= 18:
            print("青年")
        elif t33_input_int >= 8:
            print("少年")
        elif t33_input_int >= 1:
            print("儿童")
        else:
            print("年龄异常")
    except Exception as e:
        print("年龄异常")

    # TODO 34.星期一到星期日的英文，'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'
    # TODO 请根据用户输入的首字母，输出英文全部的单词，如果首字母重复，则根据用户输入的第二个单词来输出整个单词。
    t34_input = input("请输入单词首字母:")
    week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    week_temp = []
    week_final = []
    for i in week:
        if t34_input[0].upper() == i[0].upper():  # 判断加上upper，寻找单词就不区分大小写了，如果需要区分，就把upper删除
            week_temp.append(i)
    if len(week_temp) > 1:
        t34_input2 = input("请输入单词第二个字母:")
        for i in week_temp:
            i.upper()
            t34_input2.upper()
            if t34_input2[0].upper() == i[1].upper():
                week_final.append(i)
        if week_final:
            print(week_final[0])
        else:
            print("未找到对应单词")
    elif len(week_temp) == 1:
        print(week_temp[0])
    else:
        print("未找到对应单词")

    # TODO 九九乘法表打印
    for i in range(1, 10):
        for j in range(1, 10):
            print(j, "*", i, "=", i * j, " ", end=" ")  # end=" " 不换行打印
            if i == j:
                print("")
                break
